package com.gitee.ykr.lightweight.rpc.common.thread;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * 当线程池的任务缓存队列已满并且线程池中的线程数目达到maximumPoolSize时，
 * 如果还有任务到来就会采取任务拒绝策略，通常有以下四种策略：
 * ThreadPoolExecutor.AbortPolicy:丢弃任务并抛出RejectedExecutionException异常。
 * ThreadPoolExecutor.DiscardPolicy：丢弃任务，但是不抛出异常。
 * ThreadPoolExecutor.DiscardOldestPolicy：丢弃队列最前面的任务，然后重新提交被拒绝的任务
 * ThreadPoolExecutor.CallerRunsPolicy：由调用线程（提交任务的线程）处理该任务
 * @author YKR
 * @Describe
 * @date 2021/8/24
 */
public class CallerRunsPolicyWithReport extends ThreadPoolExecutor.CallerRunsPolicy{
    private String threadName;

    public CallerRunsPolicyWithReport() {
        this(null);
    }

    public CallerRunsPolicyWithReport(String threadName) {
        this.threadName = threadName;
    }

    @Override
    public void rejectedExecution(Runnable runnable, ThreadPoolExecutor executor) {
        if (threadName != null) {
            System.out.println(String.format("线程池%s已满。。。,执行者%s",threadName,executor.toString()));
        }

        super.rejectedExecution(runnable, executor);
    }
}
